/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import bundle.CountryBundle;
import comparators.InternationalizableEntityComparator;
import entities.Candidate;
import entities.CandidateLanguageInfo;
import entities.CandidateProfessionalInfo;
import entities.CompanyAdquiredProfileFolderState;
import entities.Country;
import entities.DesiredSalary;
import entities.EducationalLevel;
import entities.EducationalStatus;
import entities.JobDuration;
import entities.JobPosition;
import entities.Language;
import entities.LanguageLevel;
import entities.Sector;
import entities.Sex;
import entities.TimeAvailability;
import entities.User;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Produces;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import qualifiers.LocaleChanged;
import qualifiers.LoggedIn;
import qualifiers.ResourceList;
import qualifiers.UserResourceList;
import qualifiers.TranslatedCountryList;

/**
 *
 * @author damfercer
 */
@Named(value = "userResourcesBean")
@SessionScoped
public class UserResourcesBean implements Serializable{
    
    @Inject
    @ResourceList
    private List<EducationalStatus> educationalStatuses;
    @Inject
    @ResourceList
    private List<Sector> sectors;
    @Inject
    @ResourceList
    private List<JobDuration> jobDurations;
    @Inject
    @ResourceList
    private List<Country> countries;
    @Inject
    @ResourceList
    private List<Sex> sexs;
    @Inject
    @ResourceList
    private List<EducationalLevel> educationalLevels;
    @Inject
    @ResourceList
    private List<JobPosition> jobPositions;
    @Inject
    @ResourceList
    private List<Language> languages;
    @Inject
    @ResourceList
    private List<LanguageLevel> languageLevels;
    @Inject
    @ResourceList
    private List<DesiredSalary> desiredSalaries;
    @Inject
    @ResourceList
    private List<TimeAvailability> timeAvailabilities;
    @Inject
    @ResourceList
    private List<CompanyAdquiredProfileFolderState> companyAdquiredProfileFolderState;
    @Inject
    @LoggedIn
    User user;
    
    /**
     * Creates a new instance of ResourcesBean
     */
    public UserResourcesBean() {
    }
    
    @PostConstruct
    public void init(){
       orderUserResources();
    }
    
    public void orderUserResources(){
        Locale locale=getUserLocale();
        Collections.sort(countries, new InternationalizableEntityComparator(locale));
        Collections.sort(sexs, new InternationalizableEntityComparator(locale));
        Collections.sort(jobPositions, new InternationalizableEntityComparator(locale));
        Collections.sort(languages, new InternationalizableEntityComparator(locale));
        Collections.sort(sectors, new InternationalizableEntityComparator(locale));
    }

    public void onLocaleChanged(@Observes @LocaleChanged Language language){
        orderUserResources();
    }
    
    /*public void onLoggedInCandidateConfiguration(@Observes @LoggedIn Candidate candidate){
        orderUserResources();
    }
    
    public void onLoggedInCompanyConfiguration(@Observes @LoggedIn Company company){
        orderUserResources();
    }*/
    /**
     * @return the educationalStatuses
     */
    @Produces
    @UserResourceList
    public List<EducationalStatus> getEducationalStatuses() {
        return educationalStatuses;
    }

    /**
     * @param educationalStatuses the educationalStatuses to set
     */
    public void setEducationalStatuses(List<EducationalStatus> educationalStatuses) {
        this.educationalStatuses = educationalStatuses;
    }
    
    public Date getMaxDate(){
        Calendar date = Calendar.getInstance();  
        date.setTime(new Date());  
        date.add(Calendar.YEAR,-18); 
        return date.getTime();
    }

    /**
     * @return the sectors
     */
    @Produces
    @UserResourceList
    public List<Sector> getSectors() {
        return sectors;
    }

    /**
     * @param sectors the sectors to set
     */
    public void setSectors(List<Sector> sectors) {
        this.sectors = sectors;
    }

    /**
     * @return the jobDurations
     */
    @Produces
    @UserResourceList
    public List<JobDuration> getJobDurations() {
        return jobDurations;
    }

    /**
     * @param jobDurations the jobDurations to set
     */
    public void setJobDurations(List<JobDuration> jobDurations) {
        this.jobDurations = jobDurations;
    }

    /**
     * @return the countries
     */
    @Produces
    @UserResourceList
    public List<Country> getCountries() {
        return countries;
    }

    /**
     * @param countries the countries to set
     */
    public void setCountries(List<Country> countries) {
        this.countries = countries;
    }

    /**
     * @return the sexs
     */
    @Produces
    @UserResourceList
    public List<Sex> getSexs() {
        return sexs;
    }

    /**
     * @param sexs the sexs to set
     */
    public void setSexs(List<Sex> sexs) {
        this.sexs = sexs;
    }

    /**
     * @return the educationalLevels
     */
    @Produces
    @UserResourceList
    public List<EducationalLevel> getEducationalLevels() {
        return educationalLevels;
    }

    /**
     * @param educationalLevels the educationalLevels to set
     */
    public void setEducationalLevels(List<EducationalLevel> educationalLevels) {
        this.educationalLevels = educationalLevels;
    }

    /**
     * @return the jobPositions
     */
    @Produces
    @UserResourceList
    public List<JobPosition> getJobPositions() {
        return jobPositions;
    }

    /**
     * @param jobPositions the jobPositions to set
     */
    public void setJobPositions(List<JobPosition> jobPositions) {
        this.jobPositions = jobPositions;
    }

    /**
     * @return the languages
     */
    @Produces
    @UserResourceList
    public List<Language> getLanguages() {
        return languages;
    }

    /**
     * @param languages the languages to set
     */
    public void setLanguages(List<Language> languages) {
        this.languages = languages;
    }

    /**
     * @return the languageLevels
     */
    @Produces
    @UserResourceList
    public List<LanguageLevel> getLanguageLevels() {
        return languageLevels;
    }

    /**
     * @param languageLevels the languageLevels to set
     */
    public void setLanguageLevels(List<LanguageLevel> languageLevels) {
        this.languageLevels = languageLevels;
    }

    /**
     * @return the desiredSalaries
     */
    @Produces
    @UserResourceList
    public List<DesiredSalary> getDesiredSalaries() {
        return desiredSalaries;
    }

    /**
     * @param desiredSalaries the desiredSalaries to set
     */
    public void setDesiredSalaries(List<DesiredSalary> desiredSalaries) {
        this.desiredSalaries = desiredSalaries;
    }

    /**
     * @return the timeAvailabilities
     */
    @Produces
    @UserResourceList
    public List<TimeAvailability> getTimeAvailabilities() {
        return timeAvailabilities;
    }

    /**
     * @param timeAvailabilities the timeAvailabilities to set
     */
    public void setTimeAvailabilities(List<TimeAvailability> timeAvailabilities) {
        this.timeAvailabilities = timeAvailabilities;
    }
    
    public Locale getUserLocale(){
        Locale locale=FacesContext.getCurrentInstance().getViewRoot().getLocale();
        if(user!=null && user.getUserConfiguration()!=null){
            locale=new Locale(user.getUserConfiguration().getLanguage().getCode());
        }
        return locale;
    }
    
    public String professionalExperienceString(Candidate cand){
        String res="";
        Locale locale=getUserLocale();
        ResourceBundle rb = ResourceBundle.getBundle("messages", locale);
        ResourceBundle sb = ResourceBundle.getBundle("bundle.SectorBundle", locale);
        List<CandidateProfessionalInfo> experiences=cand.getCandidateProfessionalInfoList();
        if(experiences!=null && !experiences.isEmpty()){
            int size=experiences.size();
            if(size==1){
                res+=" "+experiences.get(0).getSector().getDescription();
            }
            else{
                int i=0;
                for(CandidateProfessionalInfo prof: experiences){
                    if(i==size-1){
                        String and=rb.getString("And");
                        res+=" "+and+" "+sb.getString(prof.getSector().getIdSector().toString());
                    }
                    else if(i==0){
                        res+=" "+sb.getString(prof.getSector().getIdSector().toString());
                    }
                    else{
                        res+=", "+sb.getString(prof.getSector().getIdSector().toString());
                    }
                    i++;
                }
            }
        }
        return res;
    }
    
    public String languageString(Candidate cand){
        String res="";
        Locale locale=getUserLocale();
        ResourceBundle rb = ResourceBundle.getBundle("messages", locale);
        ResourceBundle lb = ResourceBundle.getBundle("bundle.LanguageBundle", locale);
        List<CandidateLanguageInfo> candidateLanguages=cand.getCandidateLanguageInfoList();
        if(candidateLanguages!=null && !candidateLanguages.isEmpty()){
            int size=candidateLanguages.size();
            if(size==1){
                res+=" "+lb.getString(candidateLanguages.get(0).getLanguage().getIdLanguage().toString());
            }
            else{
                int i=0;
                for(CandidateLanguageInfo prof: candidateLanguages){
                    if(i==size-1){
                        String and=rb.getString("And");
                        res+=" "+and+" "+lb.getString(prof.getLanguage().getIdLanguage().toString());
                    }
                    else if(i==0){
                        res+=" "+lb.getString(prof.getLanguage().getIdLanguage().toString());
                    }
                    else{
                        res+=", "+lb.getString(prof.getLanguage().getIdLanguage().toString());
                    }
                    i++;
                }
            }
        }
        return res;
    }

    /**
     * @return the companyAdquiredProfileFolderState
     */
    @Produces
    @UserResourceList
    public List<CompanyAdquiredProfileFolderState> getCompanyAdquiredProfileFolderState() {
        return companyAdquiredProfileFolderState;
    }

    /**
     * @param companyAdquiredProfileFolderState the companyAdquiredProfileFolderState to set
     */
    public void setCompanyAdquiredProfileFolderState(List<CompanyAdquiredProfileFolderState> companyAdquiredProfileFolderState) {
        this.companyAdquiredProfileFolderState = companyAdquiredProfileFolderState;
    }
    
    @Produces
    @TranslatedCountryList
    public List<Country> getTranslatedCountryList(){
        
        List<Country>validCountryList=new ArrayList<Country>();
        CountryBundle countryBundle=new CountryBundle();
        List<Country>countryList=getCountries();
        for(Country country:countryList){
            if(country.getIdCountry()==77 || country.getIdCountry()==111|| country.getIdCountry()==181||country.getIdCountry()==209||country.getIdCountry()==236){
                country.setDescription(countryBundle.getString(country.getId().toString()));
                validCountryList.add(country);
            }
        }
        return validCountryList;
    }
}
